package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.domain.po.dbgrouprel.DbGroupRelPo;
import com.bwda.dsrs.filecenter.domain.condition.dbgrouprel.DbGroupRelCondition;
import com.bwda.dsrs.filecenter.domain.form.dbgrouprel.DbGroupRelQueryForm;
import com.bwda.dsrs.filecenter.domain.form.dbgrouprel.DbGroupRelUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.dbgrouprel.DbGroupRelCreateForm;
import com.bwda.dsrs.filecenter.domain.vo.dbgrouprel.DbGroupRelVo;
import com.bwda.dsrs.filecenter.service.DbGroupRelService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;
/**
 * @author Administrator
 */
@RestController
@RequestMapping("/b/dbgrouprel")
@Api(value = "/b/dbgrouprel", tags = "数据库和组关联关系表")
public class DbGroupRelController extends BaseController {

	@Autowired
	private DbGroupRelService dbGroupRelService;


	@ApiOperation(value = "查询数据库和组关联关系表",notes = "查询数据库和组关联关系表",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DbGroupRelVo> query(@ApiParam(value = "ID", required = true)@RequestParam Integer id) throws BusinessServiceException {
		DbGroupRelPo po = dbGroupRelService.queryWithValid(id);
		DbGroupRelVo vo = CopyUtil.transfer(po, DbGroupRelVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询数据库和组关联关系表数量",notes = "查询数据库和组关联关系表数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid DbGroupRelQueryForm form) throws BusinessServiceException {
		DbGroupRelCondition condition = this.getConditionByQueryForm(form);
		int count = dbGroupRelService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询数据库和组关联关系表列表",notes = "查询数据库和组关联关系表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<DbGroupRelVo>> queryList(@ModelAttribute@Valid DbGroupRelQueryForm form) throws BusinessServiceException {
		DbGroupRelCondition condition = this.getConditionByQueryForm(form);
		List<DbGroupRelPo> poList = dbGroupRelService.queryList(condition);
		List<DbGroupRelVo> voList = CopyUtil.transfer(poList, DbGroupRelVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询数据库和组关联关系表列表(带分页)",notes = "查询数据库和组关联关系表列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<DbGroupRelVo>> queryPageList(@ModelAttribute@Valid DbGroupRelQueryForm form) throws BusinessServiceException {
		DbGroupRelCondition condition = this.getConditionByQueryForm(form);
		List<DbGroupRelVo> voList = new ArrayList<>();
		int count = dbGroupRelService.queryCount(condition);
		if (count > 0) {
			List<DbGroupRelPo> poList = dbGroupRelService.queryList(condition);
			voList = CopyUtil.transfer(poList, DbGroupRelVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增数据库和组关联关系表",notes = "新增数据库和组关联关系表",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DbGroupRelVo> add(@ModelAttribute@Valid DbGroupRelCreateForm form) throws BusinessServiceException {
		DbGroupRelPo po = CopyUtil.transfer(form, DbGroupRelPo.class);
//		po.setId(RandomUtils.nextInt());
		dbGroupRelService.insert(po);
		DbGroupRelVo vo = CopyUtil.transfer(po, DbGroupRelVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改数据库和组关联关系表",notes = "修改数据库和组关联关系表",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid DbGroupRelUpdateForm form) throws BusinessServiceException {
		DbGroupRelPo po = CopyUtil.transfer(form, DbGroupRelPo.class);
		dbGroupRelService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除数据库和组关联关系表",notes = "删除数据库和组关联关系表",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "ID", required = true)@RequestParam Integer id) throws BusinessServiceException {
		dbGroupRelService.delete(id);
		return getSuccessResult();
	}


	/**
	 * DbGroupRelQueryForm转换为DbGroupRelCondition
	 *
	 * @param form
	 * @return
	 */
	private DbGroupRelCondition getConditionByQueryForm(DbGroupRelQueryForm form) {
		DbGroupRelCondition condition = CopyUtil.transfer(form, DbGroupRelCondition.class);
		return condition;
	}

}